import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shooter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shooter extends Actor
{



    int atkTimer;
    int defaultTime;
    /**
     * Act - do whatever the Shooter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Shooter()
    {
        atkTimer = 20;
        defaultTime = 20;
    }
    public void act() 
    {
        walk();
        setRotation((int)mouseDirection());
        shoot();
        atkTimer--;
        checkDead();
    }    
    
    public void walk()
    {
        if (Greenfoot.isKeyDown( "w" )) setLocation(this.getX(), this.getY() - 3 );
        if (Greenfoot.isKeyDown( "a" )) setLocation(this.getX() - 3, this.getY());
        if (Greenfoot.isKeyDown( "s" )) setLocation(this.getX(), this.getY() + 3 );
        if (Greenfoot.isKeyDown( "d" )) setLocation(this.getX() + 3, this.getY());
    }
    
    protected double mouseDirection()
    {
        MouseInfo mi = Greenfoot.getMouseInfo();
        if (mi != null){
            double dX = (mi.getX()-this.getX());
            double dY =  mi.getY() - this.getY();
            if (mi.getX() != this.getX()){
                if (mi.getX() < this.getX()){
                    return 180+180/3.14159265*Math.atan(dY/dX);
                }
                else if (mi.getX() > this.getX()){
                    return 180/3.14159265*Math.atan(dY/dX);
                }
            }
            else if (this.getY() > (mi.getY()))
            {
                return 270;
            }
            else return 90;
        }
        return (double)this.getRotation();
    }
    
    public void shoot() 
    {  
        MouseInfo mi = Greenfoot.getMouseInfo();
        if (mi != null) 
        {
            if (mi.getButton() == 1 && atkTimer <= 0)
            {
                getWorld().addObject(new Bullet((int)mouseDirection()), this.getX(), this.getY());
                atkTimer = defaultTime;
            }
        }
    }
    public void checkDead()
    {
        Enemy e = (Enemy)getOneIntersectingObject(Enemy.class);
        if (e != null)
        {
            getWorld().removeObject(this);
        }
    }
    
}
